package com.weitu.permission.role.service.impl;

import com.weitu.base.entity.Tree;
import com.weitu.permission.role.entity.Role;
import com.weitu.permission.role.mapper.RoleMapper;
import com.weitu.permission.role.service.RoleService;
import com.weitu.permission.roleGroup.entity.RoleGroup;
import com.weitu.permission.roleGroup.mapper.RoleGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/3/17.
 */
@Service
public class RoleServiceImpl implements RoleService {
    private final RoleMapper roleMapper;
    private final RoleGroupMapper roleGroupMapper;

    @Autowired
    public RoleServiceImpl(RoleMapper roleMapper,RoleGroupMapper roleGroupMapper){
        this.roleMapper=roleMapper;
        this.roleGroupMapper=roleGroupMapper;
    }

    @Override
    public List<Role> queryList(Map<String, Object> map) {
        return this.roleMapper.queryList(map);
    }

    @Override
    public int queryCount(Map<String, Object> map) {
        return this.roleMapper.queryCount(map);
    }

    @Override
    public List<Role> queryListAll() {
        return this.roleMapper.queryListAll();
    }

    @Override
    public Role queryRoleById(String id) {
        return this.roleMapper.queryRoleById(id);
    }

    @Override
    public int insertRole(Role role) {
        return this.roleMapper.insertRole(role);
    }

    @Override
    public int deleteRole(String id) {
        return this.roleMapper.deleteRole(id);
    }

    @Override
    public int updateRole(Role role) {
        return this.roleMapper.updateRole(role);
    }

    @Override
    public List<Tree> roleAndRoleGroupTree(Map map){
        List<Tree> parentTreeList = new ArrayList<Tree>();
        List<Tree> childrenTreeList;

        List<RoleGroup> roleGroupList = this.roleGroupMapper.queryRoleGroupListAll();

        Tree roleGroupTree;
        Tree roleTree;


        for (RoleGroup roleGroup : roleGroupList) {
            String roleGroupId = roleGroup.getId();
            String roleGroupName = roleGroup.getName();
            List<Role> roleList = roleMapper.queryRoleList(roleGroupId);
            if(roleList.size()>0){
                map.put("groupId",roleGroupId);
                List<Role> roleCheckedList=roleMapper.queryRoleCheckedList(map);
                roleGroupTree = new Tree();
                roleGroupTree.setId(roleGroupId);
                roleGroupTree.setText(roleGroupName);
                roleGroupTree.setChecked("true");
                childrenTreeList = new ArrayList<Tree>();
                for (Role role : roleList) {
                    String roleId = role.getId();
                    String roleName = role.getName();
                    roleTree = new Tree();
                    roleTree.setId(roleId);
                    roleTree.setText(roleName);
                    roleTree.setChecked(null);
                    for(Role roleChecked:roleCheckedList){
                        if(roleId.equals(roleChecked.getId())){
                            roleTree.setChecked("true");
                        }
                    }
                    if(roleTree.getChecked()==null){
                        roleGroupTree.setChecked(null);
                    }
                    childrenTreeList.add(roleTree);

                }
                roleGroupTree.setChildren(childrenTreeList);
                parentTreeList.add(roleGroupTree);
            }
        }

        return parentTreeList;
    }

    @Override
    public int queryCountByCode(String code) {
        return this.roleMapper.queryCountByCode(code);
    }
}
